package ani.trainning.cacobatph.das;

import java.io.File;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import ani.trainning.cacobatph.R;

public class ListFilesAdapter extends ArrayAdapter {

	List<File>files;
	private final LayoutInflater layoutInflater;
	private final Context context;

	public ListFilesAdapter(Context context, int resource,
			List<File> files) {
		super(context, resource, files);
		this.files=files;
		this.layoutInflater=LayoutInflater.from(context);
		this.context=context;
	}
	@Override
	public int getCount() {

		return this.files.size();

	}
	@Override
	public Object getItem(int position) {
		return this.files.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	public static  class Holder {
		TextView fileName;
		ImageView image;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder;
		if(convertView==null){
			convertView=layoutInflater.inflate(R.layout.file_item, null);
			holder=new Holder();
			holder.fileName=(TextView) convertView.findViewById(R.id.textFile);
			holder.image=(ImageView)convertView.findViewById(R.id.imageFile);

			convertView.setTag(holder);
		}
		else{
			holder=(Holder) convertView.getTag();

		}

		holder.fileName.setText(files.get(position).getName());
		if(files.get(position).isDirectory())
			holder.image.setImageResource(R.drawable.folder);
		else
			holder.image.setImageResource(R.drawable.file);
		return convertView;
	}
}
